package simulateOS.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * Description：通用工具类
 * <p>Date: 2022/10/31
 * <p>Time: 19:29
 *
 * @Author SillyBaka
 **/
@Slf4j
public class CommonUtils {

    private static final boolean[] flag;
    private static final Random RANDOM;

    static {
        flag = new boolean[100];
        RANDOM = new Random();
    }

    public static int getOnlyId(){
        int id;
        do {
            id = RANDOM.nextInt(100);
        }while (flag[id]);
        flag[id] = true;
        return id;
    }

    public static void releaseOnlyId(int id){
        if(!flag[id]){
            log.error("释放唯一id:{}失败，该id尚未分配",id);
            return;
        }
        flag[id] = false;
    }
}
